﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using Abp.UI;
using CPPProject.CPP.Dto;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace CPPProject.CPP
{
    /// <summary>
    /// CPP权限用户表，查询方法注意查询层级关系，避免批量删除删除其他公司下的用户
    /// 
    /// </summary>
    
    public class CPPAccountAppService : AsyncCrudAppService<CPPAccount, CPPAccountDto, int, PagedResultRequestDto, CreateCPPAccountDto, UpdateCPPAccountDto>
    {
        private readonly IObjectMapper _objectMapper;
        public CPPAccountAppService(
            IRepository<CPPAccount, int> repository,
            IObjectMapper objectMapper) : base(repository)
        {
            _objectMapper = objectMapper;
        }
        /// <summary>
        /// 用户登录接口
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="Password">密码</param>
        /// <returns></returns>
        public async Task<CPPAccountDto> AccountLogin(string userName, string Password)
        {
            Password = RequestNoHelper.GenerateMD5(Password);
            var lists = await  Repository.GetAll().Where(o => o.UserName == userName && o.Password == Password).ToListAsync();
            if (lists.Count >= 0)
            {
                return _objectMapper.Map<CPPAccountDto>(lists[0]);
            }
            else
            {
                throw new UserFriendlyException("用户名或者密码错误！");
            }
        }


        /// <summary>
        /// override重写增加方法，密码需要MD5加密保存
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<CPPAccountDto> CreateAsync(CreateCPPAccountDto input)
        {
            input.Password= RequestNoHelper.GenerateMD5(input.Password);
            var lists = await Repository.GetAll().Where(o => o.UserName == input.UserName || o.phone==input.phone).ToListAsync();
            if (lists.Count <= 0)
            {
                return await base.CreateAsync(input);
            }
            else
            {
                throw new UserFriendlyException(input.UserName+"或者"+input.phone+ "已经存在");
            }
        }

        /// <summary>
        /// override重写修改方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<CPPAccountDto> UpdateAsync(UpdateCPPAccountDto input)
        {
            input.Password = RequestNoHelper.GenerateMD5(input.Password);
            var lists = await Repository.GetAll().Where(o => o.UserName == input.UserName || o.phone == input.phone).ToListAsync();
            if (lists.Count <= 0)
            {
                if(string.IsNullOrEmpty(input.Password))
                {
                    input.Password = lists[0].Password;
                }
                return await base.UpdateAsync(input);
            }
            else
            {
                throw new UserFriendlyException(input.UserName + "或者" + input.phone + "已经存在");
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids">用户ID</param>
        /// <returns></returns>
        public async Task BatchDelete(long[] ids)
        {
            if (ids != null && ids.Count() > 0)
            {
                await Repository.DeleteAsync(it => ids.Contains(it.Id));
            }
        }



    }
}
